package com.zh.freechat.assemble;

import com.zh.freechat.common.resp.GroupMemberVO;
import com.zh.freechat.common.resp.ChatGroupVO;
import com.zh.freechat.domain.message.entity.GroupMemberPO;
import com.zh.freechat.domain.message.entity.ChatGroupPO;
import com.zh.freechat.domain.user.entity.ChatUser;
import com.zh.freechat.domain.user.service.ChatUserService;
import com.zh.webcommon.common.support.SpringContextHolder;

import java.util.List;

import static com.zh.mycommon.viewdata.ToWrapper.foreach;

/**
 * @author ZH
 * @date 13:50 2021/8/18
 */
public final class GroupRelationWrapper {

    public static ChatGroupVO convertVO(ChatGroupPO entity) {
        ChatGroupVO vo = new ChatGroupVO();
        vo.setRelId(entity.getId());
        vo.setGroupNo(entity.getGroupNo());
        vo.setName(entity.getName());
        vo.setAvatarUrl(entity.getAvatar());
        vo.setUsers(foreach(entity.getUsers(), GroupRelationWrapper::convertMemberVO));
        return vo;
    }

    public static GroupMemberVO convertMemberVO(GroupMemberPO member) {
        var vo = new GroupMemberVO();
        vo.setUserId(member.getUserId());
        vo.setAliasName(member.getAliasName());
        vo.setNickname(member.getNickname());
        vo.setAvatarUrl(member.getAvatarUrl());
        vo.setGender(member.getGender());
        return vo;
    }

    /**
     * 将members转成对应的chatUser对象
     *
     * @param members
     * @return
     */
    private static List<ChatUser> convertGroupMemberToGroupMemberVO(List<GroupMemberPO> members) {
        ChatUserService userService = SpringContextHolder.getBean(ChatUserService.class);
        var userIds = foreach(members, GroupMemberPO::getUserId);
        return userService.findByIds(userIds);
    }

}
